package org.kjtc.mapper;

import org.kjtc.entity.ChargingStation;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;

/**
 * @ Author     ：Yellowong
 * @ Date       ：Created in 10:51 2018/12/6
 * @ Description：
 */
public class StationProvider {

    public String getStationList(ChargingStation station) {

        String sqlStr;

        sqlStr = "charging_station_id AS chargingStationID, " +
                "charging_station_name AS chargingStationName, " +
                "equipment_count AS equipmentCount, " +
                "business_id AS businessID, " +
                "address, " +
                "construct_status AS constructStatus, " +
                "update_dttm AS updateDTTM " +
                "FROM charging_station " +
                "WHERE 1 = 1";

        if (!StringUtils.isEmpty(station.getChargingStationID())) {
            sqlStr += " AND charging_station_id LIKE concat(concat('%', #{chargingStationID}), '%')";
        }

        if (!StringUtils.isEmpty(station.getChargingStationName())) {
            sqlStr += " AND charging_station_name LIKE concat(concat('%', #{chargingStationName}), '%')";
        }

        if (!StringUtils.isEmpty(station.getBusinessID())) {
            sqlStr += " AND business_id LIKE concat(concat('%', #{businessID}), '%')";
        }

        if (station.getConstructStatus() >= 0) {
            sqlStr += " AND construct_status = #{constructStatus}";
        }

        SQL sql = new SQL().SELECT(sqlStr);
        sql.ORDER_BY("charging_station_id ");

        return sql.toString();
    }

    /* 统计各个状态的个数 */
    public String stationStatusStatistics() {

        return "SELECT construct_status AS status, COUNT(*) AS num FROM charging_station GROUP BY construct_status";
    }
}
